#include "friendquestitem.h"
#include "ui_friendquestitem.h"


extern QString serverIp;
extern int serverPort;
extern userinfo user;
extern QMap <QString, bool> temfriendlist; // 临时好友容器（名称, 是否在线）

friendquestItem::friendquestItem(const QString& mess, QWidget* parent)
    :ui(new Ui::friendquestItem),
    QWidget(parent)
{
    ui->setupUi(this);

    ui->label->setText(mess);

}

friendquestItem::~friendquestItem()
{
    delete ui;
}


// 同意
void friendquestItem::on_pushButton_yes_clicked()
{
    qDebug() << "同意好友申请..." << endl;
    tcpSocket = new QTcpSocket();
    tcpSocket->abort();
    tcpSocket->connectToHost(serverIp, serverPort);
    if(!tcpSocket->waitForConnected(3000))
    {
        // 连接失败，强制下线，重新进入登录界面
        QMessageBox::warning(this, "Warning!", "网络错误13", QMessageBox::Yes);
        this->close();
        user.islogin = false;
        exit(-1);
//        client *cli = new client();
//        cli->show();
    }
    else
    {
        emit delItem();
        // 服务器连接成功
        // dealwithfriendquest##yes或no##我方id##我方name##客方name
        QString fname = ui->label->text().section("申请", 0, 0);
        QString pre = "dealwithfriendquest##yes##";
        QString req = pre + QString::number(user.id) + "##" + user.name + "##" + fname;
        qDebug() << "同意好友申请，req: " << req << endl;
        tcpSocket->write(req.toUtf8());
        tcpSocket->flush();
        auto it = temfriendlist.find(fname);
        if(it != temfriendlist.end())
        {
            qDebug() << "同意好友申请，该好友原为临时好友，现从临时好友容器中删除" << endl;
            qDebug() << "原临时好友表:" << temfriendlist.size() << endl;
            QMap<QString, bool>::iterator it = temfriendlist.begin();
            while(it != temfriendlist.end())
            {
                qDebug() << it.key();
                ++it;
            }
            if(!temfriendlist.remove(fname))
            {
                qDebug() << "一个没删" << endl;
            }
            qDebug() << endl;
            qDebug() << "现临时好友表：" << temfriendlist.size() << endl;
            it = temfriendlist.begin();
            while(it != temfriendlist.end())
            {
                qDebug() << it.key();
                ++it;
            }
        }
    }
}


// 拒绝
void friendquestItem::on_pushButton_no_clicked()
{
    tcpSocket = new QTcpSocket();
    tcpSocket->abort();
    tcpSocket->connectToHost(serverIp, serverPort);
    if(!tcpSocket->waitForConnected(3000))
    {
        // 连接失败，强制下线，重新进入登录界面
        QMessageBox::warning(this, "Warning!", "网络错误13", QMessageBox::Yes);
        this->close();
        user.islogin = false;
        exit(-1);
//        client *cli = new client();
//        cli->show();
    }
    else
    {
        emit delItem();
        // 服务器连接成功
        // dealwithfriendquest##yes或no##我方id##我方name##客方name
        QString fname = ui->label->text().section("申请", 0, 0);
        QString pre = "dealwithfriendquest##no##";
        QString req = pre + QString::number(user.id) + "##" + user.name + "##" + fname;
        tcpSocket->write(req.toUtf8());
        tcpSocket->flush();
    }
}
